package com.xisoft.handle;

import com.xisoft.base.BaseModel;
import com.xisoft.common.ProcessorException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author xzh
 * @Date 2021/8/16
 * @description 处理链的默认实现
 * 注意：本内容仅限于上海熙软科技有限公司内部，传阅，禁止外泄以及用于其他商业目的
 */
public class HandlerChainImp implements HandlerChain {

    private List<Handler> handlers = new ArrayList<Handler>();

    private Iterator<Handler> iterator = null;

    private boolean iteratorFlag = true;

    @Override
    public void handle(BaseModel model) throws ProcessorException {
        if (iteratorFlag) {
            iterator = handlers.iterator();
            iteratorFlag = false;
        }
        if (iterator.hasNext()) {
            iterator.next().handle(model, this);
        }

    }

    @Override
    public void addHandler(Handler handler) {
        handlers.add(handler);
    }

}
